package com.tqjc.execute.crane.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
import com.tqjc.execute.crane.entity.CraneRoller;
import com.tqjc.execute.crane.mapper.CraneRollerMapper;
import com.tqjc.execute.crane.service.CraneRollerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Description: 天车辊道对应表服务实现
 * @author: pzy
 * @date: 2023年05月18日 10:09
 */


@Service
public class CraneRollerServiceImpl implements CraneRollerService{
    @Resource
    private CraneRollerMapper craneRollerMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CraneRoller queryById(String id){
        return craneRollerMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param craneRoller 筛选条件
     * @param current 当前页码
     * @param size  每页大小
     * @return
     */
    @Override
    public Page<CraneRoller> paginQuery(CraneRoller craneRoller, long current, long size){
        //1. 构建动态查询条件
        LambdaQueryWrapper<CraneRoller> queryWrapper = new LambdaQueryWrapper<>();
        //2. 执行分页查询
        Page<CraneRoller> pagin = new Page<>(current , size , true);
        IPage<CraneRoller> selectResult = craneRollerMapper.selectByPage(pagin , queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }

    /**
     * 新增数据
     *
     * @param craneRoller 实例对象
     * @return 实例对象
     */
    @Override
    public CraneRoller insert(CraneRoller craneRoller){
        craneRollerMapper.insert(craneRoller);
        return craneRoller;
    }

    /**
     * 更新数据
     *
     * @param craneRoller 实例对象
     * @return 实例对象
     */
    @Override
    public CraneRoller update(CraneRoller craneRoller){
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<CraneRoller> chainWrapper = new LambdaUpdateChainWrapper<CraneRoller>(craneRollerMapper);
        //2. 设置主键，并更新
        chainWrapper.set(CraneRoller::getId, craneRoller.getId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if(ret){
            return queryById(craneRoller.getId());
        }else{
            return craneRoller;
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id){
        int total = craneRollerMapper.deleteById(id);
        return total > 0;
    }
}
